package com.lianzt.util;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetUtil {
	public static boolean checkNet(Context context)
	{// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理） 
	    try { 
	        ConnectivityManager connectivity = (ConnectivityManager) context 
	                .getSystemService(Context.CONNECTIVITY_SERVICE); 
	        if (connectivity != null) { 
	            // 获取网络连接管理的对象 
	            NetworkInfo info = connectivity.getActiveNetworkInfo(); 
	            if (info != null&& info.isConnected()) { 
	                // 判断当前网络是否已经连接 
	                if (info.getState() == NetworkInfo.State.CONNECTED) { 
	                    return true; 
	                }        }        } 
	    } catch (Exception e) { 
	} 
	      return false; 
	}
   //从一个URL取得一个图片
	public static BitmapDrawable getImageFromUrl(URL url)
	{BitmapDrawable icon=null;
	  try{
		 HttpURLConnection hc=(HttpURLConnection)url.openConnection(); 
		 icon=new BitmapDrawable(hc.getInputStream()); 
		 hc.disconnect();  
	  }catch(Exception e){}
	 return icon;	
	}
	

	public static String getTimeDiff(Date date) {
		Calendar cal = Calendar.getInstance();
		long diff = 0;
		Date dnow = cal.getTime();
		String str = "";
		diff = dnow.getTime() - date.getTime();
		
		System.out.println("diff---->"+date);

		if (diff > 24 * 60 * 60 * 1000) {
			//System.out.println("1天前");
			str="1天前";
		} else if (diff > 5 * 60 * 60 * 1000) {
			//System.out.println("2小时前");
			str="2小时前";
		} else if (diff > 1 * 60 * 60 * 1000) {
			//System.out.println("1小时前");
			str="小时前";
		} else if (diff > 30 * 60 * 1000) {
			//System.out.println("30分钟前");
			str="30分钟前";
		} else if (diff > 15 * 60 * 1000) {
			//System.out.println("15分钟前");
			str="15分钟前";
		} else if (diff > 5 * 60 * 1000) {
			//System.out.println("5分钟前");
			str="5分钟前";
		} else if (diff > 1 * 60 * 1000) {
			//System.out.println("1分钟前");
			str="1分钟前";
		}else{
			str="刚刚";
		}
		

		return str;
	}

}
